package net.sevear.chaos.vo
{
	import com.adobe.serialization.json.JSON;
	import com.douban.utils.Format;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;
	
	import net.sevear.chaos.download.DBRadioKeeper;
	
	public class TrackInfo extends EventDispatcher
	{
		public static const RECOMMEND:String = "Recommend";
		public static const INSTANT:String = "Instant";
		public static const LOCAL:String = "Local";
		public static const ADTYPE:String = "T";
		
		public static const UPDATE_DOUBAN_URL:String = "listen url has updated!";
		public static const FAILED_UPDATE_DOUBAN_URL:String = "Failed in updating douban url";
		
		public var name:String = "";
		public var artistName:String = "";
		public var albumName:String = "";
		public var albumImgURL:String = "";
		public var albumDBURL:String = "";
		public var aid:int;
		public var public_time:String="";
		public var company:String= "";
		public var length:int;
		
		public var rating_avg:Number = 0;
		public var like:Boolean;
		
		public var sid:int;
		public var ssid:String="";
		public var subtype:String="";
		
		public var listenURL:String="";
		public var downURLs:Dictionary = new Dictionary();
		
		public function TrackInfo():void{
			super(null);
		}
		
		public static function isValidUrl(info:TrackInfo,sourceidx:int):Boolean{
			return info.downURLs[sourceidx]!=null && info.downURLs[sourceidx]!="";
		}
		
		public static function getSourceUrl(info:TrackInfo,url:String):int{
			if(info.downURLs[DBRadioKeeper.SOURCE_DB_RADIO] == url) 
				return DBRadioKeeper.SOURCE_DB_RADIO;
			else if(info.downURLs[DBRadioKeeper.SOURCE_XIAMI] == url)
				return DBRadioKeeper.SOURCE_XIAMI;
			else if(info.downURLs[DBRadioKeeper.SOURCE_SINA] == url)
				return DBRadioKeeper.SOURCE_SINA;
			else if(info.downURLs[DBRadioKeeper.SOURCE_GOOGLE] == url)
				return DBRadioKeeper.SOURCE_GOOGLE;
			else 
				return -1;
		}
		
		private var trackURLLoader:URLLoader;
		public function getDBRadioURL():void{
			var requrl:String = Format.radioListURL() + "&start="+this.sid+"g"+this.ssid+"g0"+"&type=n" + "&h=" + "&channel=0";
			trackURLLoader = new URLLoader();
			trackURLLoader.addEventListener(Event.COMPLETE,onGetDBRadioURL);
			trackURLLoader.addEventListener(IOErrorEvent.IO_ERROR,onIOError);
			trackURLLoader.load(new URLRequest(requrl));
		}
		
		private function onGetDBRadioURL(e:Event):void{
			var rst:*;
			try{
				rst = JSON.decode((e.target as URLLoader).data as String);
			}catch(e:Error){
				dispatchEvent(new Event(FAILED_UPDATE_DOUBAN_URL));
			}
			
			var jsonSongs:Array = rst.song as Array;
			if(jsonSongs==null || jsonSongs.length == 0){
				dispatchEvent(new Event(FAILED_UPDATE_DOUBAN_URL));
			}else{
				var song:Object = jsonSongs[0] as Object;
				if(song!=null){
					if(this.sid == parseInt(song.sid as String)){
						this.downURLs[DBRadioKeeper.SOURCE_DB_RADIO] = song.url as String;
						updateSongInfo(song);
						dispatchEvent(new Event(UPDATE_DOUBAN_URL));
					}else{
						dispatchEvent(new Event(FAILED_UPDATE_DOUBAN_URL));
					}
				}else{
					dispatchEvent(new Event(FAILED_UPDATE_DOUBAN_URL));
				}
			}
		}
		
		private function updateSongInfo(song:Object):void{
			if(this.aid.toString()!=song.aid){
				this.aid = parseInt(song.aid as String);
			}
			if(this.albumName!=song.albumtitle){
				this.albumName = song.albumtitle as String;
			}
			if(this.albumDBURL!=song.album){
				this.albumDBURL = song.album as String;
			}
			if(this.artistName != song.artist){
				this.artistName = song.artist as String;
			}
			if(this.company!=song.company){
				this.company = song.company as String;
			}
			if(this.length!=song.length){
				this.length = song.length as int;
			}
			if(this.like!=(song.like==1)){
				this.like = song.like==1;
			}
			if(this.albumImgURL != song.picture){
				this.albumImgURL = (song.picture as String).replace(/[sm]pic/,"lpic")
			}
			if(this.public_time != song.public_time){
				this.public_time = song.public_time as String;
			}
			if(this.rating_avg!=song.rating_avg){
				this.rating_avg=song.rating_avg as Number;
			}
			if(this.name != song.title){
				this.name = song.title as String;
			}
		}
		
		private function onIOError(e:IOErrorEvent):void{
			dispatchEvent(new Event(FAILED_UPDATE_DOUBAN_URL));
		}
	}
}